package com.example.mobile2030606148tdh0622;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private TextView tvMaintitle;
    private Button btnMainLaert;
    private Button btnMainApi;
    private Button btnMain6days;
    private Button btnMainCondition;
    private Button btnMainLimit;
    private TextView tvMainData;
OkHttpClient okHttpClient=new OkHttpClient();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainCondition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        RequestBody requestBody=new FormBody.Builder()
                        .add("cityID","917")
                        .add("token",MojiDataManager.CONDITIONTOKEN)
                        .build();
                        Request request=new Request.Builder()
                                .addHeader("Authorization","APPCODE"+MojiDataManager.APPCODE)
                                .url(MojiDataManager.CONDITIONURL)
                                .post(requestBody)
                                .build();
                        Call call=okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result=response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        tvMainData.setText(result);
                                        Log.i("TAG","run:"+result);
                                    }
                                });
                            }
                        });
                    }
                }).start();
            }
        });
    }

    private void initView() {
        tvMaintitle = (TextView) findViewById(R.id.tvMaintitle);
        btnMainLaert = (Button) findViewById(R.id.btnMainLaert);
        btnMainApi = (Button) findViewById(R.id.btnMainApi);
        btnMain6days = (Button) findViewById(R.id.btnMain6days);
        btnMainCondition = (Button) findViewById(R.id.btnMainCondition);
        btnMainLimit = (Button) findViewById(R.id.btnMainLimit);
        tvMainData = (TextView) findViewById(R.id.tvMainData);
    }
}